package com.xxx.store.utils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class GeneratorCode {
    public static void main(String[] args) {
        AutoGenerator autoGenerator = new AutoGenerator();

        //配置策略
        // 1. 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        //设置代码生成的位置
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java")
                .setFileOverride(true) //是否覆盖
                .setServiceName("%sService")//去除IService的I前缀
                .setIdType(IdType.AUTO) //主键的递增策略
                .setDateType(DateType.ONLY_DATE) // 仅仅是日期，没有
                .setFileOverride(false) //文件覆盖
                .setOpen(false)
        ;
        autoGenerator.setGlobalConfig(globalConfig);

        //2.数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/a6531toupiao?useUnicode=true&useSSL=false&characterEncoding=utf8");
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        autoGenerator.setDataSource(dataSourceConfig);

        //3. 包的配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName("")
                .setParent("com.xxx.store")
                .setEntity("entity")
                .setMapper("mapper")
                .setService("service")
                .setController("controller");
        autoGenerator.setPackageInfo(packageConfig);

        //4.策略配置
        StrategyConfig strategy = new StrategyConfig();
//        strategy.setInclude("notify_msg"); //配置要生成的表名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        autoGenerator.setStrategy(strategy);

        autoGenerator.execute();
    }
}